﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.AppFramework;
using XDesigner.ORM ;
using 刘颜红销售项目管理.Business.DOM;
using XDesigner.AppFramework.DOM;

namespace 刘颜红销售项目管理.Business
{
    /// <summary>
    /// 应用程序框架对象
    /// </summary>
    [System.Drawing.ToolboxBitmap( typeof( MyApplicationFramework ))]
    [MainAppFrameworkType()]
    public class MyApplicationFramework : AppFrameworkClass
    {
        private static MyApplicationFramework _Instance = new MyApplicationFramework();
        /// <summary>
        /// 对象唯一静态实例
        /// </summary>
        public static MyApplicationFramework Instance
        {
            get
            {
                return _Instance; 
            }
        } 

        /// <summary>
        /// 初始化对象
        /// </summary>
        public MyApplicationFramework()
        {
            base.ORMEngine.AddTypes( new Type[]{ 
                    typeof(Class产品型号),
                    typeof(Class费用信息),
                    typeof(Class工作单位),
                    typeof(Class人员基本信息),
                    typeof(Class项目基本信息),
                    typeof(Class工作日志)});

            this.Title = "销售管理";
        }


        private ORMBindingSource _单位列表 = null;

        /// <summary>
        /// 系统中所有单位的列表
        /// </summary>
        public ORMBindingSource 单位列表
        {
            get
            {
                if (_单位列表 == null)
                {
                    _单位列表 = this.ORMEngine.CreateBindingSource( 
                        typeof( Class工作单位 ) ,
                        this.ORMEngine.ReadAllInstances( typeof( Class工作单位 )));
                    _单位列表.AddOuterReference();
                }
                return _单位列表; 
            }
            set
            {
                _单位列表 = value; 
            }
        }

        private ORMBindingSource _项目列表 = null;
        /// <summary>
        /// 系统中所有项目信息的列表
        /// </summary>
        public ORMBindingSource 项目列表
        {
            get 
            {
                if (_项目列表 == null)
                {
                    _项目列表 = this.ORMEngine.CreateBindingSource(
                        typeof(Class项目基本信息),
                        this.ORMEngine.ReadAllInstances(typeof(Class项目基本信息)));
                    _项目列表.AddOuterReference();
                }
                return _项目列表; 
            }
            set
            {
                _项目列表 = value; 
            }
        }
        /// <summary>
        /// 清空数据缓存区
        /// </summary>
        public void ClearBuffer()
        {
            this.ORMEngine.Buffer.Clear();
            Class未分类工作单位.Instance.人员列表 = null;
            strNotifyMessage = null;
            this._项目列表 = null;
            this._单位列表 = null;
            System.GC.Collect();
        }

        private string strNotifyMessage = null;
        /// <summary>
        /// 系统提示信息
        /// </summary>
        public string NotifyMessage
        {
            get
            {
                if (strNotifyMessage == null)
                {
                    // 查询特殊日期表,进行提前10天的信息提示
                    QueryConditionList conditions = new QueryConditionList();
                    DateTime dtm = DateTime.Today.AddDays(10);
                    conditions.AddCondition("mid( 日期 , 5 , 4 )", "<=", dtm.ToString("MMdd"));
                    conditions.AddCondition("mid( 日期 , 5 , 4 )", ">=", DateTime.Today.ToString("MMdd"));
                    Array list = this.ORMEngine.ReadInstances(typeof(Class特殊日期), conditions);
                    if (list.Length > 0)
                    {
                        StringBuilder str = new StringBuilder();
                        foreach (Class特殊日期 item in list)
                        {
                            str.AppendLine(item.日期.ToString("yyyy-MM-dd dddd") + " " + item.人员.姓名 + " " + item.类型);
                        }
                        strNotifyMessage = str.ToString().Trim();
                    }
                    else
                    {
                        strNotifyMessage = "";
                    }
                }
                return strNotifyMessage;
            }
            set
            {
                strNotifyMessage = value;
            }
        }

        
    }
}
